﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.Mvvm.Messaging.Messages;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Messenger.ViewModels
{
    public partial class StudentFormViewModel : ObservableRecipient, IRecipient<ValueChangedMessage<bool>>
    {
        bool allowNew;

        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string? name;
        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string? _class;
        [ObservableProperty]
        [NotifyPropertyChangedRecipients]
        string? phone;

        public override string ToString() 
        {
            return $"Name:{Name},Class:{Class},Phone:{Phone}"; 
        }

        [RelayCommand(CanExecute =nameof(CanAddNew))]
        void AddNew()
        {
            WeakReferenceMessenger.Default.Send(new ValueChangedMessage<string>($"{Name},{Class},{Phone}"));
        }
        bool CanAddNew() => allowNew;
        public void Receive(ValueChangedMessage<bool> message)
        {
            allowNew = message.Value;
            AddNewCommand.NotifyCanExecuteChanged();
        }
    }
}
